<template>
  <view class="container">
    <div class="top-block">
      <div class="title">卡片信息</div>
      <div class="row">
        <div class="label">业务号码</div>
        <div class="value color">{{ info.business_code }}</div>
      </div>
      <div class="row">
        <div class="label">ICCID</div>
        <div class="value color">{{ info.iccid }}</div>
      </div>

      <block v-if="info.card_status">
        <div class="row">
          <div>语音</div>
          <div class="flex-set">
            <span class="color">{{ `已用 ${info.used_voice}分钟 / 总计 ${info.dose ? info.dose.voice : '??'}分钟` }}</span>
          </div>
        </div>
        <progress
          :color="(info.used_voice / (info.dose ? info.dose.voice : 0)) * 100 >= 100 ? 'red' : 'blue'"
          :percent="(info.used_voice / (info.dose ? info.dose.voice : 0)) * 100"
        />
        <div class="row">
          <div>流量</div>
          <div>
            <span class="color">{{ `已用 ${info.used_flow_size}MB / 总计 ${info.dose ? info.dose.flow : '??'}MB` }}</span>
          </div>
        </div>
        <progress
          :color="(info.used_flow_size / (info.dose ? info.dose.flow : 0)) * 100 >= 100 ? 'red' : 'blue'"
          :percent="(info.used_flow_size / (info.dose ? info.dose.flow : 0)) * 100"
        />
      </block>
    </div>

    <div class="tab-wrapper" v-if="info.card_status">
      <div class="item" :class="{ active: activeKey === 1 }" @click="activeKey = 1">
        <image class="img" src="/static/服务器.png"></image>
        <div class="text">已购套餐</div>
      </div>
      <!-- <div class="item" :class="{ active: activeKey === 2 }" @click="activeKey = 2">
        <image class="img" src="/static/联盟链.png"></image>
        <div class="text">套餐续费</div>
      </div> -->
      <div class="item" :class="{ active: activeKey === 3 }" @click="activeKey = 3">
        <image class="img" src="/static/时间周期.png"></image>
        <div class="text">已购加油包</div>
      </div>
      <div class="item" :class="{ active: activeKey === 4 }" @click="activeKey = 4">
        <image class="img" src="/static/数字化.png"></image>
        <div class="text">购买加油包</div>
      </div>
    </div>

    <!-- 已购套餐 -->
    <div class="list-block" v-if="activeKey === 1">
      <div class="item-wrap" v-for="(item, index) in info.meal_used" :key="index">
        <image v-if="isExpired(item.end_time)" class="expired-icon position-set" src="/static/已过期.png" mode="widthFix"></image>
        <div class="row">
          <div class="font-l">{{ item.meal_info.name }}</div>
          <div class="color">{{ item.meal_info.call_mins }}分钟+{{ item.meal_info.flow ? item.meal_info.flow + 'MB' : '无限流量' }}/月</div>
        </div>
        <div class="line"></div>
        <div class="date-range">
          <div>生效时间</div>
          <div>{{ item.start_time }}</div>
        </div>
        <div class="date-range">
          <div>结束时间</div>
          <div>{{ item.end_time }}</div>
        </div>
      </div>
    </div>

    <!-- 套餐购买/续费 -->
    <div class="list-block" v-if="activeKey === 2">
      <div class="title" v-if="!info.card_status">选择套餐</div>

      <div class="item-wrap" v-for="(item, index) in info.meals" :key="index" @tap="$app.goPage(`/pages/payInfo/index?cid=${id}&mid=${item.meal_id}`)">
        <div class="row">
          <div class="font-l">{{ item.meal.name }}</div>
          <div class="color">{{ item.meal.call_mins }}分钟+{{ item.meal.flow ? item.meal.flow + 'MB' : '无限流量' }}/月</div>
        </div>
        <div class="row">
          <div>
            低至 <span class="color">￥{{ (item.meal.meal_price / item.meal.meal_period).toFixed(2) }}</span
            >/月
          </div>
          <div>
            <span class="color">{{ item.meal.meal_period }}</span
            >个月飞享套餐
          </div>
        </div>
        <div class="line"></div>
        <div class="row">
          <div class="del">￥{{ item.meal.origin_price.toFixed(2) }}</div>
          <div>
            ￥<span class="font-xl color">{{ item.meal.meal_price.toFixed(2) }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 已购加油包 -->
    <div class="list-block" v-if="activeKey === 3">
      <div class="item-wrap" v-for="(item, index) in info.package_used" :key="index">
        <image v-if="isExpired(item.end_time)" class="expired-icon position-set" src="/static/已过期.png" mode="widthFix"></image>
        <div class="row">
          <div class="font-l">{{ item.package_info.name }}</div>
          <div class="color">
            {{ (item.package_info.flow ? item.package_info.flow + 'MB' : '') + ' ' + (item.package_info.voice ? item.package_info.voice + '分钟' : '') }}
          </div>
        </div>
        <div class="line"></div>
        <div class="date-range">
          <div>生效时间</div>
          <div>{{ item.start_time }}</div>
        </div>
        <div class="date-range">
          <div>结束时间</div>
          <div>{{ item.end_time }}</div>
        </div>
      </div>
    </div>

    <!-- 购买加油包 -->
    <div class="list-block" v-if="activeKey === 4">
      <div class="item-wrap" v-for="(item, index) in packageList" :key="index" @tap="$app.goPage(`/pages/payInfo/index?cid=${id}&pid=${item.id}`)">
        <div class="row">
          <div class="font-l">{{ item.name }}</div>
          <div class="color">
            {{ (item.flow ? item.flow + 'MB' : '') + ' ' + (item.voice ? item.voice + '分钟' : '') }}
          </div>
        </div>
        <div class="line"></div>
        <div class="row">
          <div>
            ￥<span class="font-xl color">{{ item.price.toFixed(2) }}</span>
          </div>
        </div>
      </div>
    </div>
  </view>
</template>

<script>
export default {
  data() {
    return {
      id: this.$route.query.id,
      info: {},
      activeKey: 2,
      packageList: [],
    };
  },
  created() {
    this.fetchData();
    this.fetchPackage();
  },
  methods: {
    isExpired(date) {
      return new Date().getTime() > new Date(date).getTime();
    },
    async fetchPackage() {
      const res = await this.$app.http(`/package/list`, { isSale: true });
      this.packageList = res.data.list;
    },
    async fetchData() {
      const res = await this.$app.http(`/card/getInfo`, {
        id: this.id,
      });
      if (res.data.id) {
        this.info = {
          ...res.data,
          meal_used: res.data.meal_used.reverse(),
          package_used: res.data.package_used.reverse(),
        };

        if (this.$route.query.activeKey) {
          this.activeKey = Number(this.$route.query.activeKey);
        } else {
          this.activeKey = this.info.card_status ? 1 : 2;
        }
      } else {
        this.$app.toast('找不到卡片信息');
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@mixin row {
  display: flex;
  align-items: center;
  margin: 10rpx 5rpx;
  padding: 5rpx 10rpx;
}

.container {
  .row {
    justify-content: space-between;
    @include row;

    .del {
      color: $text-color-5;
      text-decoration: line-through;
    }
  }

  .color {
    color: $text-color-3;
  }

  .title {
    font-size: $font-l;
    margin: 20rpx 10rpx;
    padding: 0rpx 20rpx;
    line-height: 1;
    font-weight: 700;
    border-left: 8rpx solid $text-color-3;
  }

  .top-block {
    padding: 10rpx 20rpx;
    border-bottom: 20rpx solid #efefef;

    .row {
      @include row;

      .label {
        margin-right: 10rpx;
      }
    }
  }

  .tab-wrapper {
    display: flex;
    justify-content: space-between;

    .item {
      padding: 20rpx;
      display: flex;
      flex-direction: column;
      align-items: center;

      &.active {
        font-weight: 700;
        border-bottom: 5rpx solid $text-color-3;
      }

      .img {
        width: 50rpx;
        height: 50rpx;
      }
    }
  }

  .list-block {
    padding: 10rpx 20rpx;

    .item-wrap {
      padding: 5rpx 10rpx;
      margin: 30rpx 10rpx;
      background-color: #fff;
      box-shadow: 0px 1px 8px rgba(#333, 0.3);
      border-radius: 15rpx;
      position: relative;

      .expired-icon {
        width: 150rpx;
      }

      .line {
        width: 100%;
        height: 1px;
        background-color: #efefef;
      }

      .date-range {
        padding: 10rpx;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
